package com.example.exam.controller;

import com.example.exam.pojo.vo.SubjectVO.SubjectStandardVO;
import com.example.exam.service.ISubjectService;
import com.example.exam.tools.BaseApiController;
import com.example.exam.tools.JsonPage;
import com.example.exam.tools.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "学科管理模块")
@Slf4j
@RequestMapping("/api/admin/education/subject")
public class SubjectController extends BaseApiController {

    @Autowired
    private ISubjectService subjectService;

    public SubjectController() {
        log.info("创建控制器：SubjectController");
    }

    @ApiOperationSupport(order = 110)
    @PostMapping("/edit")
    @ApiOperation("添加/修改学科")
    public JsonResult<String> edit(@RequestBody @Validated SubjectStandardVO subjectStandardVO) {
        if (subjectStandardVO.getId() == null) {
            log.debug("开始处理【添加/修改学科】的请求：{}", subjectStandardVO);
            subjectService.addNew(subjectStandardVO);
            return JsonResult.ok("添加学科成功！");
        } else {
            subjectService.update(subjectStandardVO);
            return JsonResult.ok("修改学科成功！");
        }
    }
    @ApiOperation("查询所有学科列表")
    @ApiOperationSupport(order = 120)
    @PostMapping("/list")
    public JsonResult<List<SubjectStandardVO>> list() {
        log.debug("开始处理【查询学科列表】的请求");
        List<SubjectStandardVO> list = subjectService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("分页查询学科列表")
    @ApiOperationSupport(order = 130)
    @PostMapping("/page")
    public JsonResult<JsonPage<SubjectStandardVO>> listByPage(@RequestBody SubjectStandardVO subjectStandardVO) {
        log.debug("开始处理【分页查询学科列表】的请求");
        PageHelper.startPage(subjectStandardVO.getPageIndex(), subjectStandardVO.getPageSize());
        List<SubjectStandardVO> list = subjectService.listByPage(subjectStandardVO);
        JsonPage<SubjectStandardVO> subjectStandardVOJsonPage = JsonPage.restPage(new PageInfo<>(list));
        return JsonResult.ok(subjectStandardVOJsonPage);
    }

    @ApiOperation("根据id删除学科")
    @ApiOperationSupport(order = 140)
    @PostMapping("/delete/{id:[0-9]+}")
    public JsonResult<String> deleteById(@PathVariable Integer id) {
        log.debug("开始处理【根据id删除学科】的请求");
        subjectService.deleteById(id);
        return JsonResult.ok("删除学科成功");
    }

    @ApiOperation("根据id查询学科")
    @ApiOperationSupport(order = 150)
    @PostMapping("/select/{id:[0-9]+}")
    public JsonResult<SubjectStandardVO> selectById(@PathVariable Integer id) {
        log.debug("开始处理【根据id查询学科】的请求");
        SubjectStandardVO standardById = subjectService.getStandardById(id);
        return JsonResult.ok(standardById);
    }


}
